<template>
  <div class="app-container">
    <el-card>
      <el-form ref="searchForm" :model="searchForm" inline size="small">
        <el-form-item label="问答内容">
          <el-input v-model="searchForm.content" placeholder="请输入问答内容" clearable />
        </el-form-item>
        <el-form-item label="发布时间" prop="startTime">
          <el-date-picker style="width: 200px" v-model="searchForm.beginTime" align="right" type="date"
            placeholder="选择开始日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd" format="yyyy 年 MM 月 dd 日">
          </el-date-picker>
        </el-form-item>
        <el-form-item>-</el-form-item>
        <el-form-item>
          <el-date-picker v-model="searchForm.endTime" align="right" type="date" placeholder="选择结束日期"
            :picker-options="pickerOptions" value-format="yyyy-MM-dd" format="yyyy 年 MM 月 dd 日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作者Id">
          <el-input v-model="searchForm.memberId" type="number" placeholder="请输入作者Id" clearable />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="searchForm.verifyStatus" placeholder="请选择审核状态" clearable size="small">
            <el-option label="待审核" value="0"></el-option>
            <el-option label="审核通过" value="1"></el-option>
            <el-option label="审核未通过" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所属类型">
          <el-select v-model="searchForm.authorType" placeholder="请选择所属类型" clearable size="small">
            <el-option label="系统发布" value="1"></el-option>
            <el-option label="用户发布" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button icon="el-icon-refresh" @click="resetForm('searchForm')">重置</el-button>
          <el-button icon="el-icon-search" type="primary" @click="searchList()">查询</el-button>
        </el-form-item>
      </el-form>
      <div>
        <el-button style="margin-bottom: 20px" size="mini" type="primary" icon="el-icon-plus"
          @click="addClick">新建问题</el-button>
        <el-button size="small" @click="deleteMany()" icon="el-icon-delete">批量删除</el-button>
      </div>

      <el-table :data="tableData.data" v-loading="listLoading" border style="width: 100%; margin-top: 20px"
        @selection-change="selectTableList" size="small">
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" prop="id" label="ID" width="80" />
        <el-table-column align="center" label="标题" prop="title" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column align="center" prop="content" label="问答内容" :show-overflow-tooltip="true" />
        <el-table-column align="center" label="类型" >
          <template slot-scope="scope">
            {{ scope.row.authorType == 1 ? '系统发布' : '用户发布' }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="nickname" label="作者昵称" />
        <el-table-column align="center" prop="memberId" label="作者id" />

        <el-table-column align="center" prop="publishTime" label="发帖时间" />
        <el-table-column align="center" prop="visitCount" label="阅读量"></el-table-column>

        <el-table-column prop="likeCount" align="center" label="点赞"></el-table-column>
        <el-table-column prop="collectCount" align="center" label="收藏"></el-table-column>
        <el-table-column align="center" label="回答">
          <template slot-scope="scope">
            <router-link :to="{ path: `/question/detail?id=${scope.row.id}` }">
              <span style="cursor: pointer; color: #3a8ee6">{{
                scope.row.commentCount
              }}</span>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <!--            verifyStatus 审核状态：0->未审核；1->审核通过；2->审核不通过-->
            <el-tag type="info" size="mini" v-if="scope.row.verifyStatus == 0">待审核</el-tag>
            <el-tag type="danger" size="mini" v-if="scope.row.verifyStatus == 2">审核未通过</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.verifyStatus == 1">审核通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="verifyTime" label="审核时间" />
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">



            
            <el-button v-if="scope.row.authorType == 1" size="mini" @click="editClick(scope.row)"
              type="text">编辑</el-button>
              <el-button v-if="scope.row.verifyStatus == 1 || scope.row.verifyStatus == 2" size="mini" @click="allpyDetail(scope.row)"
              type="text">修改审核</el-button>
            <el-button v-if="scope.row.verifyStatus == 0" size="mini" @click="allpyDetail(scope.row)"
              type="text">审核</el-button>
            <el-button size="mini" type="text" @click="detail(scope.row)">详情</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination class="pageRight" background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]" :current-page="searchForm.pageNum" :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="tableData.total">
      </el-pagination>

      <add-question ref="question" @getList="getList"></add-question>
      <verify ref="verify" @getList="getList"></verify>
    </el-card>
  </div>
</template>

<script>
import { pickerOptions } from "../../product/options";
import {
  getArticleList,
  deleteArticle,
  editArticleStatus,
  deleteArticleBatch,
  editArticleTop
} from "@/api/article";
import Tinymce from "@/components/Tinymce/index.vue";
import AddQuestion from "../question/components/addQuestion.vue";
import verify from './components/verify'
export default {
  name: "Article",
  components: {
    AddQuestion,
    Tinymce,
    verify
  },
  data() {
    return {
      props: {
        emitPath: false
      },
      pickerOptions,
      str: "",
      searchForm: {
        authorType: '',
        memberId: "",
        verifyStatus: "",
        content: "",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10
      },
      tableData: {
        data: [],
        total: 0
      },
      listLoading: false,
      deleteList: [],
      cateList: []
    };
  },
  async created() {
    await this.getList();
  },
  mounted() { },
  methods: {
    searchList() {
      this.searchForm.pageNum = 1;
      this.getList()
    },
    allpyDetail(v) {
      this.$refs.verify.openDialog(v)
    },


    editClick(v) {
      this.$refs.question.openDialog(v, 'edit')
    },


    detail(v) {
      // authorType 所属作者类型：1->系统发布；2->用户发布

      if (v.authorType == 1) {
        this.$refs.question.openDialog(v, 'detail')
      } else if (v.authorType == 2) {
        this.$refs.verify.openDialog(v, 'detail')
      }



    },

    resetForm(form) {
      // this.$refs[form].resetFields();
      this.searchForm = {
        authorType: '',
        memberId: "",
        verifyStatus: "",
        content: "",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10,
        cateId: ""
      };
      this.getList();
    },
    selectTableList(val) {
      this.deleteList = val.map(v => v.id);
    },
    addClick() {
      this.$refs.question.openDialog()
    },
    deleteMany() {
      let that = this;
      if (this.deleteList.length == 0) {
        this.$message({
          message: "请先选择要删除的项",
          type: "warning"
        });
      } else {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteArticleBatch(this.deleteList.join(",")).then(res => {
            if (res.code == 200) {
              that.$message({
                type: "success",
                message: "删除成功!"
              });
              setTimeout(() => {
                that.getList();
              }, 500);
            }
          });
        });
      }
    },
    changeStatus(row) {
      editArticleStatus({
        articleIdList: [row.id],
        publishStatus: 0
      }).then(res => {
        this.$message({
          type: "success",
          message: "修改成功!"
        });
        this.getList();
      });
    },
    handleEdit(v) {
      this.$router.push({
        path: "/article/add",
        query: {
          id: v.id
        }
      });
    },
    handleDelete(v) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteArticle(v.id).then(res => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getList();
        });
      });
    },
    handleDetail(v) {
      console.log("详情");
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getList();
    },
    addArtice() {
      this.$router.push({
        path: "/article/add"
      });
    },
    getList() {
      this.listLoading = true;
      this.searchForm.type = 4
      getArticleList(this.searchForm)
        .then(res => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.total;
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  position: relative;
}

.feedback-content {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
